#include "CHowToPlayState.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CGame.h"
#include "CXInput.h"
#include "Mail System.h"
#include "CUnit.h"
#include "CObjectManager.h"
#include "CObjGenerator.h"
#include "CEventSystem.h"

CHowToPlayState::CHowToPlayState()
{
	Type(SUB);
	m_nBGMusicID = CSGD_FModManager::GetInstance()->LoadSound("Resource/sound/Awakening.mp3");
	m_nBGImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/black.png");
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/HowToPlay.png", D3DCOLOR_XRGB(0, 0, 0));
}

CHowToPlayState::~CHowToPlayState()
{

}

CHowToPlayState* CHowToPlayState::GetInstance()
{
	static CHowToPlayState instance;
	return &instance;
}

void CHowToPlayState::Enter()
{
	if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGMusicID))
	{
		CSGD_FModManager::GetInstance()->PlaySound(m_nBGMusicID);
		CSGD_FModManager::GetInstance()->SetVolume(m_nBGMusicID, CGame::GetInstance()->MusicVol());
	}

	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CUnit>("CUnit");
	MailSystem::GetInstance()->InitMessageSystem(CGame::GetInstance()->MessageProc);

	for(int i = 0; i < 100; ++i)
		MailSystem::GetInstance()->SendMail(new SpawnUnitMenu());


	MailSystem::GetInstance()->ReadMail();
}

void CHowToPlayState::Exit()
{
	/*if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBGMusicID))
		CSGD_FModManager::GetInstance()->PlaySound(m_nBGMusicID);*/

	MailSystem::GetInstance()->ShutdownMailSystem();
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CUnit");
	ObjectGenerator<string, CBase>::GetInstance()->ShutdownObjectFactory();
	//CObjectManager::GetInstance()->Clear();
}

bool CHowToPlayState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_ESCAPE))
		CGame::GetInstance()->PopState();
	if(CXInput::GetInstance()->PressButton() && XINPUT_GAMEPAD_BACK)
		CGame::GetInstance()->PopState();
	return true;
}

void CHowToPlayState::Render()
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBGImageID, 0,0);
	
	CObjectManager::GetInstance()->Render();
	
	CSGD_TextureManager::GetInstance()->Draw(m_nImageID, 0,0, .75f, .75f);
}

void CHowToPlayState::Update(float fDelta)
{
	Input();

	MailSystem::GetInstance()->ReadMail();
	CEventSystem::GetInstance()->ProcessEvents();
	CObjectManager::GetInstance()->Update(fDelta);
}